<?php

declare (strict_types = 1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace Tbqphplib\Hyperfbase;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Container\ContainerInterface;

abstract class AbstractController {
    /**
     * @Inject
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject
     * @var ResponseInterface
     */
    protected $response;

    private $errors = [
        100000 => '账号重复',
        100100 => '登录失败',
    ];

    public function success($data, $message = 'success') {
        $code = $this->response->getStatusCode();

        return ['message' => $message, 'code' => $code, 'data' => $data];
    }

    /**
     * 请求失败.
     *
     * @param string $message
     *
     * @return array
     */
    public function failed($errorCode) {
        if (isset($this->errors[$errorCode])) {
            $message = $this->errors[$errorCode];
        } else {
            $message = '未知错误';
        }
        return ['message' => $message, 'code' => $errorCode, 'data' => ''];
    }
}
